package core.events;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import net.sourceforge.peertwitter.core.events.MessageDelivery;
import net.sourceforge.peertwitter.core.messaging.TweetMessage;

import org.jmock.Mockery;
import org.junit.Before;
import org.junit.Test;

import resources.LiveNodeHandle;
import rice.p2p.commonapi.Message;
import rice.p2p.commonapi.NodeHandle;
import rice.pastry.Id;

public class DeliverMessageTest {

	private Id id;
	private NodeHandle nodeHandle;

	@Before
	public void buildIdAndNodeHandle() {
		id = Id.build("0xabcde");
		nodeHandle = new LiveNodeHandle(id);
	}
	
	
	@Test
	public void aDeliverMessageDeliversTheCorrectMessage() {
		Mockery context = new Mockery();
		Message message = context.mock(Message.class);
		MessageDelivery deliver = new MessageDelivery(id, message);
		
		assertSame(message, deliver.getMessage());
	}
	
	@Test
	public void aDeliverMessageDeliversWithTheCorrectId() {
		Mockery context = new Mockery();
		Message message = context.mock(Message.class);
		MessageDelivery deliver = new MessageDelivery(id, message);
		
		assertSame(id, deliver.getId());
	}
	
	@Test
	public void twoDeliverMessagesAreEqualWhenTheMessagesAndIdsAreTheSame() {
		TweetMessage message = new TweetMessage(nodeHandle, System.currentTimeMillis(),"text");
		MessageDelivery deliver1 = new MessageDelivery(id, message);
		MessageDelivery deliver2 = new MessageDelivery(id, message);
		assertEquals(deliver1,deliver1);
		assertEquals(deliver1,deliver2);
		assertEquals(deliver2,deliver2);
	}
	
	
	@Test
	public void twoDeliverMessagesAreNotEqualWhenTheMessagesAreDifferent() {
		long timestamp = System.currentTimeMillis();
		
		TweetMessage message = new TweetMessage(nodeHandle, timestamp,"text");
		TweetMessage differentMessage = new TweetMessage(nodeHandle, timestamp,"different text!");
		
		MessageDelivery deliver1 = new MessageDelivery(id, message);
		MessageDelivery deliver2 = new MessageDelivery(id, differentMessage);
		
		assertFalse(deliver1.equals(deliver2));
	}
	
	@Test
	public void twoDeliverMessagesAreNotEqualWhenTheIdsAreDifferent() {
		long timestamp = System.currentTimeMillis();
		
		Id differentId = Id.build("0xadadadadaad");
		
		TweetMessage message = new TweetMessage(nodeHandle, timestamp,"text");
		
		MessageDelivery deliver1 = new MessageDelivery(id, message);
		MessageDelivery deliver2 = new MessageDelivery(differentId, message);
		
		assertFalse(deliver1.equals(deliver2));
	}
	
	
	@Test
	public void twoDeliverMessagesAreNotEqualWhenTheIdsAndMessagesAreDifferent() {
		long timestamp = System.currentTimeMillis();
		
		Id differentId = Id.build("0xadadadadaad");
		
		TweetMessage message = new TweetMessage(nodeHandle, timestamp,"text");
		TweetMessage differentMessage = new TweetMessage(nodeHandle, timestamp,"anotherText");

		
		MessageDelivery deliver1 = new MessageDelivery(id, message);
		MessageDelivery deliver2 = new MessageDelivery(differentId, differentMessage);
		
		assertFalse(deliver1.equals(deliver2));
	}

}
